﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SuperSurvey.BUS;

namespace SuperSurvey
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            if (cookie != null)
            {
                Session["UserLogin"] = cookie.Values["Username"];
            }
            checkLogin();
            LoadFriendList();
            if (Session["Logout"] != null)
            {
                lblRegister.Text = Session["Logout"].ToString();
            }

        }
        protected void LoadFriendList()
        {
            if (Session["UserLogin"] != null)
            {
                string userID = Session["UserLogin"].ToString();
                DataTable table = FriendControlBUS.GetFriendList(userID);
                gvwFriendList.DataSource = table;
                gvwFriendList.DataBind();
            }
        }

        protected void txtSearch_Load(object sender, EventArgs e)
        {
            txtSearch.Text = "";
        }

        protected void ibtnLogin_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["UserLogin"] != null)
            {
                Response.Redirect("#");

            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadFriendList();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (lblRegister.Text == "Register")
            {
                Response.Redirect("Register.aspx");
            }
            else
            {
                Session["Logout"] = "Register";
                Session["UserLogin"] = null;
                //HttpCookie cookie = new HttpCookie("UserLogin");
                Response.Cookies["UserLogin"].Expires = DateTime.Now; DateTime.Now.AddYears(-1);
                //DeleteCookie(HttpContext.Current.Request.Cookies["UserLogin"]);
                Response.Redirect("index.aspx");
            }

        }
        public void checkLogin()
        {
            if (Session["UserLogin"] != null)
            {
                lblLogin.Text = "Hi, " + Session["UserLogin"].ToString();
                lblFriendsList.Text = Session["UserLogin"].ToString() + ":: Friend List";
                lblUserInformation.Text = Session["UserLogin"].ToString() + ":: Manager";
                lblRegister.Text = "Logout";
                //Response.Redirect("index.aspx");
            }
            else
            {
                lblLogin.Text = "Login";
            }
        }
    }
}